package com.qf.smartdevice.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smartdevice.cache.AreaCache;
import com.qf.smartdevice.constans.ResultCode;
import com.qf.smartdevice.event.AreasCacheChangeEvent;
import com.qf.smartdevice.exceptions.QuaryDataException;
import com.qf.smartdevice.mapper.AreaMapper;
import com.qf.smartdevice.pojo.Areas;
import com.qf.smartdevice.service.IAreasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.geom.Area;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AreasServiceImpl implements IAreasService {
    @Autowired
    private AreaCache areaCache;

    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private AreasCacheChangeEvent event;


    @Override
    public PageInfo<Areas> findAllArea(int page, int limit, String areaname, Long status) {

//        PageHelper.startPage(page, limit);
//        从缓存中获取查询出来的所有数据
        List<Areas> allData = areaCache.getAllData();
//        从MAP集合缓存中获取查询出来有父区域的数据
        Map<Long, Areas> dataMap = areaCache.getDataMap();
        Set<Map.Entry<Long, Areas>> entries = dataMap.entrySet();
//        遍历allData集合
        for (Areas areas : allData) {
//            遍历MAP 集合
            for (Map.Entry<Long, Areas> entry : entries) {
//                获取MAP集合的值也就是 一个Areas对象
                Areas value = entry.getValue();
//              判断  父区域的Parentid 是否等于  没有父区域的ID
                if (areas.getParentid() == value.getId()) {
//                    将没有父区域的Areaname值 赋值给有父区域的Parentname
                    areas.setParentname(value.getAreaname());
                }
            }
        }

        System.out.println("wwwwwwww" + allData);
        List<Areas> areasList = allData.stream().filter(areas -> {

//            判断是否 条件查询了

//            fileter 默认保留 为true 的结果
            boolean isFetchaName = true;
            boolean isFetchStatus = true;

            String areaname1 = areas.getAreaname();
            Long status1 = areas.getStatus();
//          如果不是空的就代表 是有条件查询
            if (!StringUtils.isEmpty(areaname)) {
                isFetchaName = areaname1.contains(areaname);
            }
            if (status != null && -100 != status) {
                isFetchStatus = status == status1;
            }
            return isFetchaName && isFetchStatus;
        }).collect(Collectors.toList());

        System.out.println("hahahha" + areasList);
// ==================开始分割集合================
        PageInfo pageInfo = null;
        int last_index = limit;

        if (page * limit >= areasList.size()) {
            last_index = areasList.size();
        } else {
            last_index = page * limit;
        }

        if ((page - 1) * limit >= areasList.size()) {
            pageInfo = new PageInfo<>();
            return pageInfo;
        }

        List<Areas> areas = areasList.subList((page - 1) * limit, last_index);

        pageInfo = new PageInfo(areas);
        pageInfo.setTotal(areasList.size());//设置总长度为符合条件的集合的长度

        System.out.println("业务层" + allData);
        return pageInfo;
    }

    @Override
    public List<Areas> getAllParentArea() {
        List<Areas> allData = areaCache.getAllData();//所有的区域
        // 需要获取父区域 就是看他的 parent是不是空
        List<Areas> areasList = allData.stream().filter(
//                父区域为空的就是一级区域 所以我们过滤的时候很简单只要判断 parentid==null
                areas -> areas.getParentid() == null

        ).collect(Collectors.toList());

        System.out.println("所有的父区域为" + areasList);

        return areasList;

    }

    @Override
    public List<Areas> getAllAreasByParentId(Long parendId) {

        if (parendId == null || parendId <= 0) {
            throw new QuaryDataException("主键不存在", ResultCode.NULL_DATA);
        }
        List<Areas> allData = areaCache.getAllData();
//        获取parentid== 我们传递的ID 数据
        List<Areas> collect = allData.stream().filter(
                areas -> areas.getParentid() == parendId

        ).collect(Collectors.toList());

        System.out.println("所有的父ID" + collect);

        return collect;
    }

    @Override
    public int deleteByIdArea(int[] ids) {

        int i = areaMapper.deleteByIdArea(ids);

        areaCache.onEvent(event);
        return i;

    }

    @Override
    public int insertArea(Areas areas) {
        int i = areaMapper.insertArea(areas);
        areaCache.onEvent(event);
        return i;
    }

    @Override
    public int updateArea(Areas areas) {

        int i = areaMapper.updateByIdArea(areas);
        areaCache.onEvent(event);
        return i;

    }
}
